<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/8/7
 * Time: 15:15
 */

namespace Home\Model;
use Think\Model;
class OrderDetailedInfoModel extends Model{
    public function queryOrder($userId){
        $sql="SELECT a.`ID` AS orderId,b.ID AS detailId,a.shippingAddress_ID,a.orderDate,a.payDate,a.deliveryDate,a.isBingo,
            a.confirmReceiptDate,b.`goods_ID`,b.`unitPrice`,b.`orderNumber`,b.`customerNote`,c.goodsName,
            b.`unitPrice`*b.`orderNumber` AS totalMoney
            FROM EachGo.`t_order_info` a JOIN EachGo.`t_order_detailed_info` b ON a.`ID`=b.`orderInfo_ID`
            JOIN EachGo.`t_goods` c ON b.`goods_ID`=c.ID
            WHERE user_ID=".$userId." AND isUserDelete= 0 ORDER BY a.orderDate DESC";
        $result=$this->query($sql);
        $turn=count($result);
        $allResult=$this->complete($result,$turn);
        //var_dump($allResult);
        return $allResult;
    }
    public function queryOneOrder($orderId){
        $sql="SELECT a.`ID` AS orderId,b.ID AS detailId,a.shippingAddress_ID,a.orderDate,a.payDate,a.deliveryDate,a.isBingo,
            a.confirmReceiptDate,b.`goods_ID`,b.`unitPrice`,b.`orderNumber`,b.`customerNote`,c.goodsName,
            b.`unitPrice`*b.`orderNumber` AS totalMoney
            FROM EachGo.`t_order_info` a JOIN EachGo.`t_order_detailed_info` b ON a.`ID`=b.`orderInfo_ID`
            JOIN EachGo.`t_goods` c ON b.`goods_ID`=c.ID
            WHERE a.ID=".$orderId." AND isUserDelete= 0";
        return $this->query($sql);
    }
    public function complete($orderInfo,$turn){
        $userLotterInfo=D('UserLotteryInfo');
        //var_dump($orderInfo);
        for($i=0;$i<$turn;$i++) {
            $info=$orderInfo[$i];
            //var_dump($info);
            $numberSql="SELECT COUNT(*) AS number FROM EachGo.`t_user_lottery_info`
              WHERE goods_ID=".$info['goods_id'];//��ȡ��ǰƴ������
            $number=$userLotterInfo->query($numberSql);
            //var_dump($number);
            $joinPriceSql="SELECT joinPrice FROM EachGo.`t_user_lottery_info`
              WHERE goods_ID=".$info['goods_id'];
            $joinPrice=$userLotterInfo->query($joinPriceSql);
            $numberArray=array(
                'number'=>$number[0]['number'],
            );
            $totalNumberArray=array(
                'total'=>ceil((int)$info['unitprice']/(int)$joinPrice[0]['joinprice']),
            );
            $orderInfo[$i]=array_merge($orderInfo[$i],$numberArray);//�ϲ�����
            //$total=ceil ((int)$info['unitprice']/(int)$info['joinprice']);//�����ƴ����Ʒ��Ҫ��������
            $orderInfo[$i]=array_merge($orderInfo[$i],$totalNumberArray);
            //var_dump($orderInfo[$i]);
        }
        //var_dump($orderInfo);
        return $orderInfo;
    }
    public function queryIdEvaluated($userId){
        $sql="SELECT DISTINCT b.`orderInfo_ID` AS evaluated
            FROM EachGo.`t_order_comment_info` AS a RIGHT JOIN EachGo.`t_order_detailed_info` AS b
            ON  a.`order_Detailed_Info_ID`=b.`ID`
            WHERE  a.`date` IS NULL AND b.`orderInfo_ID`
            IN(SELECT ID FROM EachGo.`t_order_info`
            WHERE EachGo.`t_order_info`.`user_ID`=".$userId."
            AND EachGo.`t_order_info`.`confirmReceiptDate`>'1971-01-01'
            AND EachGo.`t_order_info`.`isBingo`=0)";
        return $this->query($sql);
        //var_dump($result);
    }
    public function addDetaileOrder($data){
        return $this->add($data);
    }
    public function delDetaileOrder($orderId){
        return $this->where('user_ID'.$orderId)->delete();
    }
}